package com.lagou.rpc.consumer.controller;


import com.lagou.rpc.api.IUserService;
import com.lagou.rpc.common.ZooNodeData;
import com.lagou.rpc.consumer.proxy.RpcClientProxy;
import com.lagou.rpc.pojo.User;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private RpcClientProxy rpcClientProxy;

    //http://localhost:8081/client/getUserFromServer/1
    @RequestMapping("/getUserFromServer/{id}")
    public User getUserFromServer(@PathVariable Integer id) {
        IUserService userService = (IUserService) rpcClientProxy.createProxy(IUserService.class);
        User user = userService.getById(id);
        System.out.println(user);
        return user;
    }
    //http://localhost:8081/client/shutDownServer/7000
    @RequestMapping("/shutDownServer/{port}")
    public String shutDownServer(@PathVariable Integer port) {
        IUserService userService = (IUserService) rpcClientProxy.createProxy(IUserService.class);
        String s = userService.shutDownServer("127.0.0.1", port);
        System.out.println(s);
        return s;
    }

    @Autowired
    private ZkClient zkClient;


    //http://localhost:8081/client/getFromZk/7000
    @RequestMapping("/getFromZk/{port}")
    public ZooNodeData getFromZk(@PathVariable String port) {
//        String s = zkClient.readData("/lg-zookeeper-homework/provider_127.0.0.1:7000");
//        System.out.println(s);
        ZooNodeData zooNodeData = zkClient.readData("/lg-zookeeper-homework/provider_127.0.0.1:" + port);
        System.out.println(zooNodeData.toString());
        return zooNodeData;
    }

}
